package com.lsf;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 * @author 刘愿
 * @date 2021/2/17 19:06
 * @see [相关类/方法]
 * @since V1.00
 */
public class WriteTest {

    @Test
    public void writeByPoi() throws Exception {

        String path = "D:\\JAVA\\GitHub\\apache-poi";
        //新建工作簿
        Workbook workbook = new HSSFWorkbook();
        //新建表
        Sheet sheet = workbook.createSheet("Alexander");
        //新建行
        Row row1 = sheet.createRow(0);
        //新建单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("第一行第一个单元格（1,1）");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("第一行第二个单元格（1,2）");

        Row row2 = sheet.createRow(1);
        Cell cell21 = row1.createCell(0);
        cell21.setCellValue("第二行第一个单元格（2,1）");
        Cell cell22 = row1.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        FileOutputStream fileOutputStream = new FileOutputStream(path+"writeByPoi.xls");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
    }

    @Test
    public void writeByPoi07() throws Exception {

        String path = "D:\\JAVA\\GitHub\\apache-poi";
        //新建工作簿
        Workbook workbook = new SXSSFWorkbook();
        //新建表
        Sheet sheet = workbook.createSheet("Alexander");
        //新建行
        Row row1 = sheet.createRow(0);
        //新建单元格
        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("第一行第一个单元格（1,1）");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("第一行第二个单元格（1,2）");

        Row row2 = sheet.createRow(1);
        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("第二行第一个单元格（2,1）");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));

        FileOutputStream fileOutputStream = new FileOutputStream(path+"writeByPoi07.xlsx");
        workbook.write(fileOutputStream);
        fileOutputStream.close();
        ((SXSSFWorkbook)workbook).dispose();
    }
}
